package com.sgb.kjwl.view.ui.activity.transport;

import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import com.swgk.core.util.LogUtil;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityCarsInfoJoinQuoted3Binding;
import com.sgb.kjwl.di.transport.DaggerTransportComponent;
import com.sgb.kjwl.di.transport.TransportModule;
import com.sgb.kjwl.model.entity.transport.GoodsInfoEntity3;
import com.sgb.kjwl.view.widget.GoodsQuotedPricePopWindow3;
import com.sgb.kjwl.viewmodel.transport.CarsInfoJoinQuotedModel3;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.util.MToast;

import java.util.List;

import javax.inject.Inject;

/**
 * 找车信息首页--悬窗-承运竞价
 */
public class CarsInfoJoinQuotedActivity3 extends AppActivity {

    private ActivityCarsInfoJoinQuoted3Binding binding;
    private GoodsQuotedPricePopWindow3 pricePopWindow;
    private InputMethodManager im;
    private List<GoodsInfoEntity3> list;

    @Inject
    CarsInfoJoinQuotedModel3 joinQuotedModel;

    public static void start(Context context){
        Intent intent = new Intent(context,CarsInfoJoinQuotedActivity3.class);
        context.startActivity(intent);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerTransportComponent
                .builder()
                .appComponent(appComponent)
                .transportModule(new TransportModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_cars_info_join_quoted3);
        binding.setViewModel(joinQuotedModel);
        binding.include.titleView.setText("承运竞价");
        binding.include.ivBack.setOnClickListener(this);
        binding.pushSelect.setOnClickListener(this);
        binding.rangeSelect.setOnClickListener(this);
        binding.quotedSure.setOnClickListener(this);

        //报价需求列表弹窗
        pricePopWindow = new GoodsQuotedPricePopWindow3(this);
        pricePopWindow.setBgView(binding.bgPopGrayLayout);
        pricePopWindow.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object goodsInfo, Object key2, Object key3) {
                LogUtil.v("quotedPrice","name=="+((GoodsInfoEntity3)goodsInfo).getGoodsName());
                binding.pushSelect.setText(((GoodsInfoEntity3)goodsInfo).getGoodsName());
                joinQuotedModel.entity.get().setGoodsName(((GoodsInfoEntity3)goodsInfo).getGoodsName());
                joinQuotedModel.entity.get().setGoodsInfoId(((GoodsInfoEntity3) goodsInfo).getGoodsInfoId());
            }
        });

        im = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    }

    @Override
    protected void init() {

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);

        switch (v.getId()){
            case R.id.iv_back:
                finish();
                break;
            case R.id.push_select:
            case R.id.range_select:
                if(null!=list && list.size()>0){
                    showPricePopWindow();
                }else{
                    joinQuotedModel.getQuotedPriceList();
                }
                break;
            case R.id.quoted_sure:
                save();
                break;
        }
    }

    private void save(){

        if(TextUtils.isEmpty(joinQuotedModel.price.get())){
            MToast.showToast(CarsInfoJoinQuotedActivity3.this,"请输入价格");
            return;
        }

        if(TextUtils.isEmpty(joinQuotedModel.distance.get())){
            MToast.showToast(CarsInfoJoinQuotedActivity3.this,"请输入半径");
            return;
        }

        if(TextUtils.isEmpty(joinQuotedModel.entity.get().getGoodsName())){
            MToast.showToast(CarsInfoJoinQuotedActivity3.this,"请选择需求");
            return;
        }

        if(TextUtils.isEmpty(joinQuotedModel.entity.get().getGoodsName())){
            MToast.showToast(CarsInfoJoinQuotedActivity3.this,"请选择范围");
            return;
        }

        //发起请求

        joinQuotedModel.putQuotedPrice();
    }

    public void getQuotedPriceList(List<GoodsInfoEntity3> lists){
        if(null!=lists && lists.size()>0){
            list = lists;
            showPricePopWindow();
        }
    }

    private void showPricePopWindow(){
        pricePopWindow.showPopwindowBottom(binding.quotedPriceRoot);
//        pricePopWindow.setDatas(list);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if(null!=im){
            im.hideSoftInputFromWindow(binding.orderPrice.getWindowToken(),0);
        }
        return super.dispatchTouchEvent(ev);
    }
}
